<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>解决HttpURLConnection 调用 getInputStream() 方法报异常的问题</title>
    <style type="text/css" media="all">
      body {
        margin: 0;
        font-family: "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", sans-serif;
        font-size: 14px;
        line-height: 20px;
        color: #777;
        background-color: white;
      }
      .container {
        width: 700px;
        margin-right: auto;
        margin-left: auto;
      }

      .post {
        font-family: Georgia, "Times New Roman", Times, "SimSun", serif;
        position: relative;
        padding: 70px;
        bottom: 0;
        overflow-y: auto;
        font-size: 16px;
        font-weight: normal;
        line-height: 25px;
        color: #515151;
      }

      .post h1{
        font-size: 50px;
        font-weight: 500;
        line-height: 60px;
        margin-bottom: 40px;
        color: inherit;
      }

      .post p {
        margin: 0 0 35px 0;
      }

      .post img {
        border: 1px solid #D9D9D9;
      }

      .post a {
        color: #28A1C5;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="post">
        <h1 class="title">解决HttpURLConnection 调用 getInputStream() 方法报异常的问题</h1>
        <div class="show-content">
          <p>看客朋友们大家好, 欢迎收看本期内容, 我是大表哥...</p><p>本期内容说的是关于 HttpURLConnection 调用 getInputStream() 方法报出异常的问题, 废话不多说, 看正题:</p><p>HttpURLConnection 调用 getInputStream() 方法的代码如下: </p><p>InputStream inputStream = httpURLConnection.getInputStream();</p><p>InputStream <b>getErrorStream</b>()</p><p>如果连接失败但服务器仍然发送了有用数据，则返回错误流。</p><p><br></p><p>对了，就是他，判断getResponseCode，当返回不是HttpURLConnection.HTTP_OK， HttpURLConnection.HTTP_CREATED， HttpURLConnection.HTTP_ACCEPTED 时，不能用<b>getInputStream()</b>，而是应该用<b>getErrorStream</b>()。修改代码，问题解决。</p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p><p>完美解决.</p><p>本期内容到此结束, 感谢观看, 我是大表哥, 我们下期见;</p>
        </div>
      </div>
    </div>
  </body>
</html>
